﻿using System.Text;
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

namespace GX
{
    public static class PoolManager
    {
        static IPoolManager _mgr;
        static IPoolManager mgr
        {
            get
            {
                if (null == _mgr)
                    _mgr = App.GetService(App.SERVICE_POOL_MANAGER) as IPoolManager;
                return _mgr;
            }
        }

        static public readonly int TypeHashCode_StringBuilder = typeof(StringBuilder).GetHashCode();
        static public readonly int TypeHashCode_ByteStream = typeof(ByteStream).GetHashCode();

        
        static public int defaultCapacity
        {
            get { return mgr.defaultCapacity; }
            set { mgr.defaultCapacity = value; }
        }
        static public bool Get(int typeHashCode, out object item)
        {
            return mgr.Get(typeHashCode, out item);
        }
        static public object Get(Type type)
        {
            return mgr.Get(type);
        }
        static public void Release(object item)
        {
            mgr.Release(item);
        }
        static public void GC()
        {
            mgr.GC();

        }
        static public void SetCapacity(int typeHashCode, int capacity)
        {
            mgr.SetCapacity(typeHashCode, capacity);
        }
        static public T Get<T>(Type type)
        {
            object item;
            mgr.Get(type.GetHashCode(), out item);
            return (T)item;
        }
        static public T Get<T>() where T : class,new()
        {
            var type = typeof(T);

            object item;
            if (mgr.Get(type.GetHashCode(), out item))
                return item as T;

            T ret = new T();
            return ret;
        }
        static public List<T> GetList<T>(int capacity = 0)
        {
            List<T> list = null;

            var type = typeof(List<T>);

            object item;
            if (mgr.Get(type.GetHashCode(), out item) && (item is List<T>))
            {
                list = item as List<T>;
                if (list.Count < capacity)
                    list.Capacity = capacity;
                return list;
            }
            return new List<T>(capacity);
        }
        static public Dictionary<TK, TV> GetDictionary<TK, TV>(int capacity = 0)
        {
            Dictionary<TK, TV> dict = null;

            var type = typeof(Dictionary<TK, TV>);

            object item;
            if (mgr.Get(type.GetHashCode(), out item)
                && (item is Dictionary<TK, TV>))
            {
                dict = item as Dictionary<TK, TV>;
                //if (dict.Count < capacity)
                //    dict.Capacity = capacity;
                return dict;
            }
            return new Dictionary<TK, TV>(capacity);
        }
        static public StringBuilder GetStringBuilder(int capacity = 0)
        {
            object obj;
            if (mgr.Get(TypeHashCode_StringBuilder, out obj))
            {
                var sb = obj as StringBuilder;
                if (null != sb)
                {
                    if (sb.Capacity < capacity)
                        sb.Capacity = capacity;
                    return sb;
                }
            }
            return new StringBuilder(capacity);
        }
        static public ByteStream GetByteStream(int capacity = 0)
        {
            object obj;
            if (mgr.Get(TypeHashCode_StringBuilder, out obj))
            {
                var bs = obj as ByteStream;
                if (null != bs)
                {
                    if (bs.capacity < capacity)
                        bs.capacity = capacity;
                    return bs;
                }
            }
            return new ByteStream(capacity);
        }
    }
}